<script setup lang="ts">
import axios from 'axios'
import { onMounted, ref } from 'vue'
// 定义空数组
// 定义接口类型
interface News {
  cmtcount: number
  id: number
  img: string
  source: string
  time: string
  title: string
}
// 注意：这里使用泛型去约束类型
const newList = ref<News[]>([])
const getList = async () => {
  const res = await axios.get('http://hmajax.itheima.net/api/news')
  // 注意：ref声明的必须用.value
  newList.value = res.data.data
  console.log(newList.value)
}
// 接口地址：http://hmajax.itheima.net/api/news
// 请求方式：get
onMounted(() => {
  getList()
})
</script>


<template>
  <div id="app">
    <ul>
      <li class="news" v-for="item in newList" :key="item.id">
        <div class="left">
          <div class="title">{{ item.title }}</div>
          <div class="info">
            <span>{{ item.source }}</span>
            <span>{{ item.time }}</span>
          </div>
        </div>
        <div class="right">
          <img :src="item.img" alt="" />
        </div>
      </li>
    </ul>
  </div>
</template>

<style>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}

.news {
  display: flex;
  height: 120px;
  width: 600px;
  margin: 0 auto;
  padding: 20px 0;
  cursor: pointer;
}

.news .left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-right: 10px;
}

.news .left .title {
  font-size: 20px;
}

.news .left .info {
  color: #999999;
}

.news .left .info span {
  margin-right: 20px;
}

.news .right {
  width: 160px;
  height: 120px;
}

.news .right img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>